package calculator.rpn;

import calculator.rpn.api.CalculatorService;
import calculator.rpn.engine.impl.DefaultCalculatorService;

import java.util.Scanner;

public class CalculatorMain {
    public static void main(String[] args) throws Exception{
        String cmd;

        CalculatorService calculator = new DefaultCalculatorService();

        StringBuilder sb = new StringBuilder();
        Scanner scanner = new Scanner(System.in);

        while(!(cmd = scanner.nextLine()).equals("break")){
            sb.setLength(0);

            try{
                calculator.onInput(cmd);
                String output = calculator.calculate();
                calculator.appendStateTo(sb);

                System.out.println(output);
                System.out.println(sb.toString());
            } catch(IllegalStateException | IllegalArgumentException e){
                System.out.println(e.getMessage());
                sb.setLength(0);
                calculator.appendStateTo(sb);
                System.out.println(sb.toString());
            }

        }
    }
}
